//tak arne
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;

public class DatabaseDeploy {
	private String username;
	private String password;
	private String port;
	private String rootPassword;
	private String sqlString = "";
	private String sqlFile = "";
	private Connection con;
	
	public int connect(){
		if (port.equals(""))         {return 4;}  // port not set!
		if (rootPassword.equals("")) {return 5;}  // rootPassword not set
		if (sqlFile.equals(""))      {return 6;}  // SQL-file not set
		
		try {
			Class.forName("com.mysql.jdbc.Driver"); //loader mysqconnector klassen
		} 
		catch (ClassNotFoundException e) {
			return 1;
		}
		
		try{ //forbinder til databasen, allowing multiqueries!
			String url = "jdbc:mysql://localhost:" + port;  //url += "/testdb";
			con = DriverManager.getConnection(url,"root", rootPassword);
		}	
		
		catch(SQLException e){
			System.out.println(e);
			return 2; 	// wrong root password, could not connect
						// forbedring?  f� exception fra driveren til at virke s� man kan skelne imellem intet svar og forkerte credentials
		}
		Statement s;
		try {
			s = con.createStatement();
			readFile(); // fills sqlString
			String[] sqlCommands = sqlString.split(";");
			System.out.println(sqlCommands[sqlCommands.length-1]); // Last SQL command = COMMIT?
			for (String sql: sqlCommands){
//			    System.out.println(sql); // List all SQL commands on console
				try {
					s.addBatch(sql);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					System.out.println(e);
				}
			}
			s.executeBatch();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return 0; // Everything went well...
	}		
		
	private void readFile(){
		try{
//			FileReader filereader = new FileReader(new File("????????????.sql"));
			FileReader filereader = new FileReader(new File(sqlFile));
			BufferedReader br = new BufferedReader(filereader);
			String s;
			while((s = br.readLine()) != null) { 
				sqlString += s;
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void setPassword (String password){
		this.password = password;
	}
	public void setUsername (String username){
		this.username = username;
	}
	public String getPassword(){
		return this.password;
	}
	public String getUsername(){
		return this.username;
	}
	public void setPort(String port){
		this.port = port;
	}
	public void setPort(int port){
		this.port = ""+port;
	}
	public String getPort(){
		return port;
	}
	public void setRootPassword(String rootPassword){
		this.rootPassword = rootPassword;
	}
	public void setSqlFile(String sqlFile){
		this.sqlFile = sqlFile;
	}
	
}